<!DOCTYPE html>
<html>
    <head>
        <title>Itowns - postprocessing</title>

        <meta charset="UTF-8">
        <link rel="stylesheet" type="text/css" href="css/example.css">
        <link rel="stylesheet" type="text/css" href="css/LoadingScreen.css">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div id="viewerDiv"></div>
        <script src="../dist/itowns.js"></script>
        <script src="js/GUI/LoadingScreen.js"></script>
        <!-- from https://github.com/mrdoob/three.js/blob/master/examples/js/shaders/DotScreenShader.js -->
        <script type="x-shader/x-vertex" id="vertexshader">
            varying vec2 vUv;

            void main() {
                vUv = uv;
                gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
            }
        </script>

        <script type="x-shader/x-fragment" id="fragmentshader">
            uniform vec2 center;
            uniform float angle;
            uniform float scale;
            uniform vec2 tSize;

            uniform sampler2D tDiffuse;

            varying vec2 vUv;

            float pattern() {

                float s = sin( angle ), c = cos( angle );

                vec2 tex = vUv * tSize - center;
                vec2 point = vec2( c * tex.x - s * tex.y, s * tex.x + c * tex.y ) * scale;

                return ( sin( point.x ) * sin( point.y ) ) * 4.0;
            }
            void main() {
                vec4 color = texture2D( tDiffuse, vUv );
                float average = ( color.r + color.g + color.b ) / 3.0;
                gl_FragColor = vec4( vec3( average * 10.0 - 5.0 + pattern() ), color.a );
            }
        </script>

        <script type="text/javascript">
            var placement = {
                coord: new itowns.Coordinates('EPSG:4326', 2.351323, 48.856712),
                range: 25000000,
            }


            // iTowns namespace defined here
            var viewerDiv = document.getElementById('viewerDiv');
            var view = new itowns.GlobeView(viewerDiv, placement);

            // Simple postprocessing setup
            //
            var postprocessScene = new itowns.THREE.Scene();
            var quad = new itowns.THREE.Mesh(new itowns.THREE.PlaneBufferGeometry(2, 2), null);
            var cam = new itowns.THREE.OrthographicCamera(-1, 1, 1, -1, 0, 10);

            setupLoadingScreen(viewerDiv, view);

            quad.frustumCulled = false;
            quad.material = new itowns.THREE.ShaderMaterial({
                uniforms: {
                    tDiffuse: { value: null },
                    tSize: { value: new itowns.THREE.Vector2(256, 256) },
                    center: { value: new itowns.THREE.Vector2(0.5, 0.5) },
                    angle: { value: 1.57 },
                    scale: { value: 1.0 },
                },
                vertexShader: document.getElementById('vertexshader').textContent,
                fragmentShader: document.getElementById('fragmentshader').textContent,
            });
            postprocessScene.add(quad);

            view.render = function render() {
                var g = view.mainLoop.gfxEngine;
                var r = g.renderer;
                r.setRenderTarget(g.fullSizeRenderTarget);
                r.clear();
                r.render(view.scene, view.camera.camera3D);

                quad.material.uniforms.tDiffuse.value = g.fullSizeRenderTarget.texture;
                quad.material.uniforms.tSize.value.set(
                    g.fullSizeRenderTarget.width, g.fullSizeRenderTarget.height);

                r.setRenderTarget(null);
                r.clear();
                r.render(postprocessScene, cam);
            };

            itowns.Fetcher.json('./layers/JSONLayers/Ortho.json').then(function _(config) {
                config.source = new itowns.WMTSSource(config.source);
                var layer = new itowns.ColorLayer('Ortho', config);
                view.addLayer(layer);
            });
            itowns.Fetcher.json('./layers/JSONLayers/IGN_MNT.json').then(function _(config) {
                config.source = new itowns.WMTSSource(config.source);
                var layer = new itowns.ElevationLayer(config.id, config);
                view.addLayer(layer);
            });
        </script>
    </body>
</html>
